package com.sirdc.modules.sys.web.HardWork;

import com.sirdc.modules.sys.util.MD5;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.oversee.filter.CoordinateFilter;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.CoordinateService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.databean.BnpUserDatabean;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.Branch;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.service.BnpUserService;
import net.huashitong.user.service.BranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 攻坚办审核人员  用户管理
 * Created by hyp1202 on 2018/7/4.
 */
@Controller
@RequestMapping("/hardWork")
public class SysUserManageController {

    @Autowired
    private BnpUserService bnpUserService;
    @Autowired
    private BranchService branchService;

    @ModelAttribute("headType")
    public String returnHeadType() {
        return "管理模块";
    }


    @RequestMapping(value = "/go/list", method = RequestMethod.GET)
    public String goListAdmin(@ModelAttribute BnpUserFilter filter, Model model, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        model.addAttribute("leftType", "用户管理");
        double totalPage = bnpUserService.getBnpUserSum(filter);//进一位
        model.addAttribute("totalPage", totalPage);

        int myTaskNum = (int) WebUtils.getSessionAttribute(request, "myTaskNum");
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);

        int myTackTaskNum = (int) WebUtils.getSessionAttribute(request, "myTackTaskNum");
        model.addAttribute("myTackTaskNum", myTackTaskNum);

        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        model.addAttribute("nodeCount", nodeCount);

        //责任单位
        List<String> deptList = new ArrayList<>();
        Map<String, BnpUser> deptMap = new HashMap<>();
        BnpUserFilter bnpUserFilter = new BnpUserFilter();
        bnpUserFilter.setBranchType("1");
        bnpUserFilter.setRole("经办人");
        List<BnpUser> bnpUsers = bnpUserService.queryByFilter(bnpUserFilter);
        for (BnpUser user : bnpUsers) {
            if (!deptMap.containsKey(user.getBranch())) {
                deptMap.put(user.getBranch(),user);
                deptList.add(user.getBranch());
            }
        }
        model.addAttribute("deptList", deptList);

        return "/modules/hardWork/userManger";
    }

    @ResponseBody
    @RequestMapping(value = "/go/lists", method = RequestMethod.POST)
    public Object goList(@ModelAttribute BnpUserFilter filter, Model model) {
        List<BnpUser> list = bnpUserService.getUserInfo(filter);
        double totalPage = bnpUserService.getBnpUserSum(filter);//进一位
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("model", list);
        map.put("totalPage", totalPage);
        return map;
    }


    @RequestMapping(value = "/doAddUser", method = RequestMethod.POST)
    public String addUser(@ModelAttribute BnpUser bnpUser, Model model, HttpServletRequest request,
                          RedirectAttributes redirectAttributes) {
        if (ObjectUtils.isBlank(verifyPhoneExist2(bnpUser.getPhone()))) {
            String password = bnpUser.getPassword();
            password = MD5.parseMD5(password);
            bnpUser.setPassword(password);
            bnpUserService.save(bnpUser);
            return "redirect:/hardWork/go/list";
        } else {
            redirectAttributes.addFlashAttribute("msg", "添加失败，手机号已存在");
            return "redirect:/hardWork/go/list";
        }
    }

    @ResponseBody
    @RequestMapping("/verifyPhoneExist")
    private boolean verifyPhoneExist(@RequestParam("phone") String phone) {
        return bnpUserService.getBnpUserByPhone(phone) == null;
    }

    private BnpUser verifyPhoneExist2(String phone) {
        return bnpUserService.getBnpUserByPhone(phone);
    }

    @RequestMapping("/delete")
    public String deleteUser(@ModelAttribute BnpUserFilter bnpUserFilter) {
        bnpUserService.deleteById(bnpUserFilter.getSysid());
        return "redirect:/hardWork/go/list";
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateUser(@ModelAttribute BnpUserFilter bnpUserFilter, Model model,
                             RedirectAttributes redirectAttributes) {
        BnpUser user = bnpUserService.getBnpUserByName(bnpUserFilter.getSysid());
        BnpUser bnpUser = verifyPhoneExist2(bnpUserFilter.getPhone());
        if (ObjectUtils.isBlank(bnpUser) || Objects.equals(user.getSysId(), bnpUser.getSysId())) {
            user.setName(bnpUserFilter.getName());
            user.setPhone(bnpUserFilter.getPhone());
            user.setBranchType(bnpUserFilter.getBranchType());
            user.setRole(bnpUserFilter.getRole());
            user.setBranch(bnpUserFilter.getBranch());
            bnpUserService.update(user);
        } else {
            redirectAttributes.addFlashAttribute("msg", "修改失败，手机号已存在");
        }
        BnpUserFilter filter = new BnpUserFilter();
        filter.setPage(bnpUserFilter.getPage());
        List<BnpUser> list = bnpUserService.getUserInfo(filter);
        Map<String, Object> map = new HashMap<>();
        map.put("model", list);
        return map;
    }

    @ResponseBody
    @RequestMapping("/showBranch")
    public List<Branch> getBranch(HttpServletRequest request) {
        String branchType = request.getParameter("branch_type");
        List<Branch> branch = branchService.getBranchByType(branchType);
        return branch;
    }

}
